﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Downloader
{
    
    class ConfigFile
    {
        private string m_filename;
        public ConfigFile(string filename,Download dl,out bool success)
        {
            m_filename = filename;
            //新下载任务
            if (!File.Exists(filename))
            {
                //创建并写入配置
                dl.m_downloadedProgress = -1;
                if (dl.m_url.StartsWith("http://"))
                {
                    dl.m_filelength = HttpUtil.getHttpFilelength(dl.m_url);
                    if (dl.m_filelength == -2 || dl.m_filelength == -3)
                    {
                        success = false;
                        return;
                    }
                    dl.m_bRangable = HttpUtil.testRangeAvailable(dl.m_url);
                    if (!dl.m_bRangable)
                    {
                        //dl.m_blocksize = 0;
                        dl.m_downloadThreadCount = 1;
                    }
                } 
                else if(dl.m_url.StartsWith("ftp://"))
                {
                    dl.m_filelength = FtpUtil.getFtpFilelength(dl.m_url);
                    dl.m_bRangable = true;
                }
                File.Create(dl.m_localpath+'\\'+dl.m_localfilename).Close();
                WriteConfig(dl);
            }
            ReadConfig(dl);
            success = true;
        }
        public bool ReadConfig(Download dl)
        {

            StreamReader sr = new StreamReader(new FileStream(this.m_filename, FileMode.Open));
            //读取配置
            try
            {
                dl.m_url = sr.ReadLine();
                dl.m_localfilename = sr.ReadLine();
                dl.m_filelength = int.Parse(sr.ReadLine());
                dl.m_blocksize = int.Parse(sr.ReadLine());
                dl.m_downloadedProgress = int.Parse(sr.ReadLine());
                dl.m_downloadProgress = dl.m_downloadedProgress + 1;
                dl.m_downloadThreadCount = int.Parse(sr.ReadLine());
                dl.m_maxblock = dl.m_filelength / dl.m_blocksize;
                if (dl.m_filelength % dl.m_blocksize != 0)
                    dl.m_maxblock += 1;
                dl.m_maxblock -= 1;
            }
            catch (System.Exception ex)
            {
                sr.Close();
                return false;
            }
            
            sr.Close();
            return true;
        }
        //写入下载配置文件
        public bool WriteConfig(Download dl)
        {
            //if (dl.m_url == null)
            //{
            //    return false;
            //}
            StreamWriter sw = new StreamWriter(new FileStream(this.m_filename, FileMode.Create));
            sw.WriteLine(dl.m_url);
            sw.WriteLine(dl.m_localfilename);
            sw.WriteLine(dl.m_filelength);
            sw.WriteLine(dl.m_blocksize);
            sw.WriteLine(dl.m_downloadedProgress);
            sw.WriteLine(dl.m_downloadThreadCount);
            sw.Close();
            return true;
        }

        static public string ReadUrl(string filename)
        {
            StreamReader sr = new StreamReader(new FileStream(filename, FileMode.Open));
            string s =  sr.ReadLine();
            sr.Close();
            return s;
        }
        //static public string ReadBlockSize(string filename)
        //{
        //    StreamReader sr = new StreamReader(new FileStream(filename, FileMode.Open));
        //    sr.ReadLine();
        //    sr.ReadLine();
        //    sr.ReadLine();
        //    string s = sr.ReadLine();
        //    sr.Close();
        //    return s;
        //}
    }
}
